package org.corey.dev.voxiverse.chunk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


import org.corey.dev.voxiverse.Player;
import org.corey.dev.voxiverse.block.Block;
import org.corey.dev.voxiverse.body.Body;

public class Save{
	
	private static Block[][][] checkBlocks;
	public static String lastFileName;
	public static Block[][][] test;
	

	public Save(){
		
	}
	
	public static void Load(Block[][][] blocks, String filename){
		try{
			FileInputStream saveFile = new FileInputStream(System.getenv("APPDATA") + "\\Voxiverse\\" + filename + ".chunk");
			
			ObjectInputStream save = new ObjectInputStream(saveFile);
			
			for (int x = 0; x < Chunk.CHUNK_SIZE_X;x++){
				for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++){
						
					for (int y = 0; y < Chunk.CHUNK_SIZE_Y; y++){
						
						blocks[x][y][z] = (Block) save.readObject();//first fill every block with an unrendered air block

					}
				}
			}
			
			
			save.close();
		} catch(Exception exc) {
		}
		
		
	}
	
	
	
	

	public static boolean CheckChunk(String filename) {
		try{
			File dir = new File(System.getenv("APPDATA") + "\\Voxiverse");
			File file = new File(System.getenv("APPDATA") + "\\Voxiverse\\" + filename + ".chunk");
			
			if (!(dir.exists())){
				new File(System.getenv("APPDATA") + "\\Voxiverse").mkdir();
			}
			
			if (!(file.exists())){
				return false;
			} else {
				return true;
			}
		}
		catch(SecurityException se){
		}
		catch(Exception exc) {
		}
		
		return false;
		
	}

	public static void SaveChunk(Block[][][] blocks, String filename) {
		
		
		try {
			FileOutputStream saveFile = new FileOutputStream(System.getenv("APPDATA") + "\\Voxiverse\\" + filename + ".chunk");
			ObjectOutputStream save = new ObjectOutputStream(saveFile);
			
			for (int x = 0; x < Chunk.CHUNK_SIZE_X;x++){
				for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++){	
					for (int y = 0; y < Chunk.CHUNK_SIZE_Y; y++){
					
						save.writeObject(blocks[x][y][z]); //first fill every block with an unrendered air block

					}
				}
			}
			
			save.close();
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
		
		
		
		
		
	}
	
	public static int CheckNextChunk(int x, int y, int z, int ChunkX, int ChunkZ){
		//test =  Body.Chunks[(Player.ChunkX / (Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH))][(Player.ChunkZ / (Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH))].blocks;
		if (CheckChunk(Player.filename)){
			checkBlocks = Body.Chunks[(ChunkX / (Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH))][(ChunkZ / (Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH))].blocks;
			return checkBlocks[(x - (Chunk.CHUNK_SIZE_X * (ChunkX / (Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH)))) + 1][y][(z - (Chunk.CHUNK_SIZE_Z * (ChunkZ / (Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH)))) + 1].GetID();
		} else {
			return 0;
		}
		
	}
	
	public static int CheckNextChunk(int x, int y, int z, String filename){
		//System.out.println(filename +  " = " + lastFileName);
		if (lastFileName == filename) {
			System.out.println("Speedy");
			return checkBlocks[x][y][z].GetID();
		} else {
			System.out.println("Slow");
			
			lastFileName = filename;
			checkBlocks = new Block[Chunk.CHUNK_SIZE_X * Body.WORLD_SIZE][Chunk.CHUNK_SIZE_Y][Chunk.CHUNK_SIZE_Z * Body.WORLD_SIZE];
			if (CheckChunk(filename)){
				System.out.println("Yes" + filename);
				//System.out.println("New File Open");
				Load(checkBlocks, filename);
				
				System.out.println("Cant Load");
		
				return checkBlocks[x][y][z].GetID();
			} else {
				lastFileName = null;
				return 0;
			}
		
		}
		
	}
}

